﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Content;
using System.Diagnostics;

namespace TomAndJerry
{

    class Instruction4 : GameScreen
    {

        Texture2D backgroundTexture;
        Texture2D backHomeTexture;
        Texture2D rightInstrucTexture;
        Texture2D leftInstrucTexture;
        Texture2D soundTexture;
        Texture2D musicTexture;

        Vector2 backHomePosition;
        Vector2 leftInstrucPosition;
        Vector2 rightInstrucPosition;
        Vector2 soundPosition;
        Vector2 musicPosition;
        /// <summary>
        /// Constructor.
        /// </summary>
        public Instruction4()
            : base()
        {
            //sound effect for game
            //AudioManager.PushMusic("LoseTheme",false);
            this.Exiting += new EventHandler(Instruction4_Exiting);
        }


        void Instruction4_Exiting(object sender, EventArgs e)
        {
            //stop music
            //AudioManager.PopMusic();
            Debug.WriteLine("Debug: exiting");
        }

        public override void LoadContent()
        {
            ContentManager content = ScreenManager.Game.Content;
            Viewport viewport = ScreenManager.GraphicsDevice.Viewport;

            backgroundTexture = content.Load<Texture2D>(@"Backgrounds\huongdan4");
            backHomeTexture = content.Load<Texture2D>(@"Items\BackHome");
            rightInstrucTexture = content.Load<Texture2D>(@"Items\ArrowRight");
            leftInstrucTexture = content.Load<Texture2D>(@"Items\ArrowLeft");
            soundTexture = content.Load<Texture2D>(@"Items\sound_ena");
            musicTexture = content.Load<Texture2D>(@"Items\music_ena");

            backHomePosition = new Vector2(100, 320);
            leftInstrucPosition = new Vector2(100, 250);
            rightInstrucPosition = new Vector2(90, 450);
            soundPosition = new Vector2(50, 10);
            musicPosition = new Vector2(50, 60);
        }

        /// <summary>
        /// UnloadContent
        /// </summary>

        public override void UnloadContent()
        {
            base.UnloadContent();
        }

        /// <summary>
        /// Handles user input.
        /// </summary>
        public override void HandleInput()
        {
            int clickMode = 0;
            ///back click
            if (InputManager.IsButtonClicked(new Rectangle(
                (int)backHomePosition.X - backHomeTexture.Height, (int)backHomePosition.Y,
                (int)(backHomeTexture.Height),
                (int)(backHomeTexture.Width))))
            {
                clickMode = 1;
                Debug.WriteLine("Touch");
            }

            ///left click
            if (InputManager.IsButtonClicked(new Rectangle(
                (int)leftInstrucPosition.X - leftInstrucTexture.Height, (int)leftInstrucPosition.Y,
                (int)(leftInstrucTexture.Height),
                (int)(leftInstrucTexture.Width))))
            {
                clickMode = 2;
                Debug.WriteLine("Touch");
            }

            ///right click
            if (InputManager.IsButtonClicked(new Rectangle(
                (int)rightInstrucPosition.X - rightInstrucTexture.Height, (int)rightInstrucPosition.Y,
                (int)(rightInstrucTexture.Height),
                (int)(rightInstrucTexture.Width))))
            {
                clickMode = 3;
                Debug.WriteLine("Touch");
            }


            ///sound click
            if (InputManager.IsButtonClicked(new Rectangle(
                (int)soundPosition.X - soundTexture.Height, (int)soundPosition.Y,
                (int)(soundTexture.Height),
                (int)(soundTexture.Width))))
            {
                clickMode = 4;
                Debug.WriteLine("Touch");
            }

            ///music click
            if (InputManager.IsButtonClicked(new Rectangle(
                (int)musicPosition.X - musicTexture.Height, (int)musicPosition.Y,
                (int)(musicTexture.Height),
                (int)(musicTexture.Width))))
            {
                clickMode = 5;
                Debug.WriteLine("Touch");
            }

            GameScreen screen = null;
            switch (clickMode)
            {
                case 1://back home
                    screen = new HomeScreen();
                    break;
                case 2://left arrow
                    screen = new Instruction3();
                    break;
                case 3://right arrow
                    screen = new Instruction5();
                    break;
                case 4://sound
                    ///<<enable/disable sound>>
                    break;
                case 5://music
                    ///<<enable/disable music>>
                    break;
                default:
                    break;
            }
            if (screen != null)
            {
                ExitScreen();
                ScreenManager.AddScreen(screen);
            }
            base.HandleInput();
        }

        /// <summary>
        /// Updates the menu.
        /// </summary>
        public override void Update(GameTime gameTime, bool otherScreenHasFocus,
                                                       bool coveredByOtherScreen)
        {

            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }


        /// <summary>
        /// Draws the menu.
        /// </summary>
        public override void Draw(GameTime gameTime)
        {
            SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
            ScreenManager.GraphicsDevice.SetRenderTarget(ScreenManager.FinalImageTarget);
            ScreenManager.GraphicsDevice.Clear(Color.CornflowerBlue);

            // we will do all of our drawing here
            ScreenManager.GraphicsDevice.SetRenderTarget(null);

            spriteBatch.Begin();
            spriteBatch.Draw(backgroundTexture, new Vector2(480, 0), null, Color.White, MathHelper.PiOver2, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
            spriteBatch.Draw(backHomeTexture, backHomePosition, null, Color.White, MathHelper.PiOver2, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
            spriteBatch.Draw(leftInstrucTexture, leftInstrucPosition, null, Color.White, MathHelper.PiOver2, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
            spriteBatch.Draw(rightInstrucTexture, rightInstrucPosition, null, Color.White, MathHelper.PiOver2, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
            spriteBatch.Draw(soundTexture, soundPosition, null, Color.White, MathHelper.PiOver2, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
            spriteBatch.Draw(musicTexture, musicPosition, null, Color.White, MathHelper.PiOver2, Vector2.Zero, Vector2.One, SpriteEffects.None, 0f);
            spriteBatch.End();
        }
    }
}
